package ofmybatis.demo.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

@Service
public class TestTransaction extends TransactionTemplateSupport  {

    @Autowired
    private JdbcTemplate jdbcTemplate ;

    @Autowired
    private TransactionTemplate transactionTemplate;

   // @Transactional
    public void test(){

        jdbcTemplate.execute("insert into user value (1,'aaa','aaa','aaa')");

        int i = 1/0;

        jdbcTemplate.execute("insert into user value (2,'aaa','aaa','aaa')");

    }


    public void test2(){

       getRequiresNewTransactionTemplate()
     //   transactionTemplate
                .execute(new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus status) {


                jdbcTemplate.execute("insert into user value (11,'BBBB','aaa','aaa')");

                int i = 1/0;

                jdbcTemplate.execute("insert into user value (21,'aaa','NNNN','aaa')");


                return null;
            }
        });
    }


}
